package com.hjw.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hjw.entity.Characterx;
import com.hjw.entity.Skill;
import com.hjw.mapper.SkillMapper;
import com.hjw.service.ICharacterxService;
import com.hjw.service.ISkillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjw.util.PageResult;
import com.hjw.vo.SkillVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hjw
 * @since 2025-01-12
 */
@Service
public class SkillServiceImpl extends ServiceImpl<SkillMapper, Skill> implements ISkillService {

    @Autowired
    private ICharacterxService characterService;
    @Override
    public PageResult<Skill> findByPage(Integer page, Integer pageSize) {
        Page<Skill> page1 = this.page(new Page<>(page, pageSize));
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    @Override
    public PageResult<Skill> search(Integer page, Integer pageSize, SkillVo vo) {
        if (vo.getCharacterId() == null){
            Page<Skill> page1 = this.page(new Page<>(page, pageSize));
            return new PageResult<>(page1.getRecords(),page1.getTotal());
        }
        QueryWrapper<Skill> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getSkillName()), "skill_name",  vo.getSkillName() )
                .eq(vo.getCharacterId() != null && vo.getCharacterId() != 0, "character_id", vo.getCharacterId());
        Page<Skill> page1 = this.page(new Page<>(page, pageSize), wrapper);
        page1.getRecords().forEach(st -> {
            Characterx characterx = characterService.getById(st.getCharacterId());
            st.setCharacterName(characterx.getCharacterName());
        });
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
}
